package parkingSpace

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/cars"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/garage"
	"github.com/flipped-aurora/gin-vue-admin/server/model/parkingSpace"
	parkingSpaceReq "github.com/flipped-aurora/gin-vue-admin/server/model/parkingSpace/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

type ParkingSpaceApi struct {
}

var parkingSpacesService = service.ServiceGroupApp.ParkingSpaceServiceGroup.ParkingSpaceService

// CreateParkingSpace 创建parkingSpace表
// @Tags ParkingSpace
// @Summary 创建parkingSpace表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body parkingSpace.ParkingSpace true "创建parkingSpace表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /parkingSpaces/createParkingSpace [post]
func (parkingSpacesApi *ParkingSpaceApi) CreateParkingSpace(c *gin.Context) {
	var parkingSpaces parkingSpace.ParkingSpace
	err := c.ShouldBindJSON(&parkingSpaces)
	fmt.Println(parkingSpaces)
	fmt.Println(parkingSpaces.GarageId)
	fmt.Println("111")
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := parkingSpacesService.CreateParkingSpace(&parkingSpaces); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteParkingSpace 删除parkingSpace表
// @Tags ParkingSpace
// @Summary 删除parkingSpace表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body parkingSpace.ParkingSpace true "删除parkingSpace表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /parkingSpaces/deleteParkingSpace [delete]
func (parkingSpacesApi *ParkingSpaceApi) DeleteParkingSpace(c *gin.Context) {
	var parkingSpaces parkingSpace.ParkingSpace
	err := c.ShouldBindJSON(&parkingSpaces)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := parkingSpacesService.DeleteParkingSpace(parkingSpaces); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteParkingSpaceByIds 批量删除parkingSpace表
// @Tags ParkingSpace
// @Summary 批量删除parkingSpace表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除parkingSpace表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /parkingSpaces/deleteParkingSpaceByIds [delete]
func (parkingSpacesApi *ParkingSpaceApi) DeleteParkingSpaceByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := parkingSpacesService.DeleteParkingSpaceByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateParkingSpace 更新parkingSpace表
// @Tags ParkingSpace
// @Summary 更新parkingSpace表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body parkingSpace.ParkingSpace true "更新parkingSpace表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /parkingSpaces/updateParkingSpace [put]
func (parkingSpacesApi *ParkingSpaceApi) UpdateParkingSpace(c *gin.Context) {
	var parkingSpaces parkingSpace.ParkingSpace
	err := c.ShouldBindJSON(&parkingSpaces)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := parkingSpacesService.UpdateParkingSpace(parkingSpaces); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindParkingSpace 用id查询parkingSpace表
// @Tags ParkingSpace
// @Summary 用id查询parkingSpace表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query parkingSpace.ParkingSpace true "用id查询parkingSpace表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /parkingSpaces/findParkingSpace [get]
func (parkingSpacesApi *ParkingSpaceApi) FindParkingSpace(c *gin.Context) {
	var parkingSpaces parkingSpace.ParkingSpace
	err := c.ShouldBindQuery(&parkingSpaces)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reparkingSpaces, err := parkingSpacesService.GetParkingSpace(parkingSpaces.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		var resData parkingSpace.ParkingSpaceRes
		db := global.GVA_DB.Model(&garage.Garage{})
		var gdata garage.Garage
		db.Where("id = ?", reparkingSpaces.GarageId).First(&gdata)

		resData = parkingSpace.ParkingSpaceRes{
			GVA_MODEL: global.GVA_MODEL{
				ID:        reparkingSpaces.ID,
				CreatedAt: reparkingSpaces.CreatedAt,
			},
			Name:            gdata.Name,
			GarageId:        reparkingSpaces.GarageId, //garageId字段
			ParkingSpaceNum: reparkingSpaces.ParkingSpaceNum,
			Area:            reparkingSpaces.Area,     //车位面积
			Floor:           reparkingSpaces.Floor,    //楼层
			Category:        reparkingSpaces.Category, //车位类型
			Status:          reparkingSpaces.Status,
		}

		response.OkWithData(gin.H{"reparkingSpaces": resData}, c)
	}
}

// GetParkingSpaceList 分页获取parkingSpace表列表
// @Tags ParkingSpace
// @Summary 分页获取parkingSpace表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query parkingSpaceReq.ParkingSpaceSearch true "分页获取parkingSpace表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /parkingSpaces/getParkingSpaceList [get]
func (parkingSpacesApi *ParkingSpaceApi) GetParkingSpaceList(c *gin.Context) {
	var pageInfo parkingSpaceReq.ParkingSpaceSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := parkingSpacesService.GetParkingSpaceInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		var datas []*parkingSpace.ParkingSpaceRes
		for _, v := range list {
			db := global.GVA_DB.Model(&garage.Garage{})
			var data garage.Garage
			db.Where("id = ?", v.GarageId).First(&data)
			fmt.Println(data)
			var carData cars.Car
			err := global.GVA_DB.Model(&cars.Car{}).Where("parking_space_id = ?", v.ID).First(&carData).Error
			if err == gorm.ErrRecordNotFound {
				carData.OwnerName = ""
			}
			datas = append(datas, &parkingSpace.ParkingSpaceRes{
				GVA_MODEL: global.GVA_MODEL{
					ID:        v.ID,
					CreatedAt: v.CreatedAt,
				},
				Name:            data.Name,
				UserName:        carData.OwnerName,
				GarageId:        v.GarageId, //garageId字段
				ParkingSpaceNum: v.ParkingSpaceNum,
				Area:            v.Area,     //车位面积
				Floor:           v.Floor,    //楼层
				Category:        v.Category, //车位类型
				Status:          v.Status,
			})
		}
		response.OkWithDetailed(response.PageResult{
			List:     datas,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (parkingSpacesApi *ParkingSpaceApi) GetParkingSpaceListNoPage(c *gin.Context) {
	var parkingSpaces parkingSpace.ParkingSpace
	err := c.ShouldBindQuery(&parkingSpaces)
	fmt.Println(parkingSpaces)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, err := parkingSpacesService.GetParkingSpaceInfoListNoPage(*parkingSpaces.GarageId); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		fmt.Println(list)
		response.OkWithDetailed(response.PageResult{
			List: list,
		}, "获取成功", c)
	}
}
